﻿using Furion.DependencyInjection;
using OnWork.DataCenter.BLL.Base;
using OnWork.DataCenter.BLL.Base.Dto;
using OnWork.DataCenter.DAL;
using OnWork.DataCenter.DAL.DataCenter;
using OnWork.DataCenter.Utils.Caches;
using SqlSugar;

namespace OnWork.DataCenter.BLL.DataCenter
{
    /// <summary>
    /// 数据标准业务
    /// </summary>
    public class DataStandardBLL : BaseBLL<DataStandard>, IScoped
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SQLReturn> AddDataAsync(DataStandard input)
        {
            if (string.IsNullOrEmpty(input.Name))
            {
                return SQLReturn.AddError($"错误:配置名称不能为空!");
            }

            input.Name = input.Name.Trim();

            bool isExit = await base.Queryable().Where(x => x.Name == input.Name).AnyAsync();
            if (isExit)
            {
                return SQLReturn.AddError($"错误:数据连接名称:{input.Name}已存在!");
            }
            var rows = await base.AddAsync(input); //添加
            return SQLReturn.AddSuccess();
        }

        /// <summary>
        /// 批量复制
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SQLReturn> CopyDataAsync(List<DataStandard> input)
        { 
            //复制时将名称周增加随机字符
            var Name = DateTime.Now.ToString("fff");
            foreach (var item in input)
            {
                item.Name = item.Name.Trim() + Name;
            }
            var rows = await base.AddAsync(input); //添加
            return SQLReturn.AddSuccess(rows.Count);
        }


        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<SQLReturn> DeleteDataAsync(long[] ids)
        {
            if (ids == null || ids.Length == 0)
            {
                return SQLReturn.DelError($"错误:删除Id不存在!");
            }
            var rows = await base.DeleteAsync<DataStandard>(ids);
            ClearCache(); //清理缓存
            return SQLReturn.DelSuccess(rows);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SQLReturn> UpdateDataAsync(DataStandard input)
        {
            if (string.IsNullOrEmpty(input.Name))
            {
                return SQLReturn.UpdateError($"错误:配置名称不能为空!");
            }
            var old = await base.SingleByIdAsync(input.ID);
            input.Name = input.Name.Trim();
            if (!string.IsNullOrEmpty(input.Name))
            {
                if (Queryable().Where(x => x.Name == input.Name && x.Name != old.Name).Any())
                {
                    return SQLReturn.UpdateError($"错误:数据连接名称:{input.Name}已存在!");
                }
            }
            var rows = await base.UpdateAsync(input);
            ClearCache();

            return SQLReturn.UpdateSuccess(rows);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        public async Task<SQLReturn<List<DataStandard>>> QueryPagedListAsync(BaseQueryPageInput option)
        {
            var querysql = option.ToQuerySQL<DataStandard>();

            RefAsync<int> total = 0;
            var query = await Queryable<DataStandard>()
                .WhereIF(!string.IsNullOrEmpty(querysql.Where), querysql.Where)
                .OrderByDescending(x => x.CreateTime)
                .OrderByIF(!string.IsNullOrEmpty(querysql.Order), $"{querysql.Order}")
                .ToPageListAsync(option.PageIndex, option.PageItems, total);
            return SQLReturn<DataStandard>.QueryListSuccess(query, total.Value);
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        public void ClearCache()
        {
            MemoryCacheUtil.Removes(CacheKeys.BLLMqttDataStandard);
            MemoryCacheUtil.Removes(CacheKeys.BLLDataStandard); //清理缓存
        }

        /// <summary>
        /// 获取全部的类型 有缓存
        /// </summary>
        /// <returns></returns>
        public static List<DataStandard> GetALLStandardsCache()
        {
            ////使用内存缓存
            string _timeCacheKey = CacheKeys.BLLDataStandard + "/GetALLStandardsCache";
            return MemoryCacheUtil.GetOrCreate(_timeCacheKey, entry =>
            {
                return DBManger.DataCenter.CopyNew().Queryable<DataStandard>().Where(x => x.IsDel == false).ToList();
            }
            );
        }
    }
}